package ModelLayer;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.io.FileNotFoundException;


/**
 * Write a description of class FileOrder here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class FileOrder
{
   private Order orders;
   private File file;
   private BufferedWriter bw;
   private FileWriter fw;
   private BufferedReader br;
   private FileReader fr;
   private ArrayList<Order> old_orders;
   
   public FileOrder()
   {
       file = new File("Orders.txt");
       if(!(file.exists()))
       {
           try
           {
               file.createNewFile();
            }
            
            catch(IOException e)
            {
                
            }
        }
       old_orders = get_data();
        
    }
    public boolean save_changes(ArrayList<Order> new_orders)
    {
        if(has_changed(new_orders))
        {
            try
            {
             fw = new FileWriter(file);
             bw = new BufferedWriter(fw);
             for(Order order : new_orders)
             {
                 String line = order.give_data_string();
                 bw.write(line);
                 bw.newLine();
                }
                bw.close();
                old_orders = get_data();
                return true;
            }
            catch (IOException e)
            {
                System.out.println("HA, just crashed on IOException");
            }
        }
        return false;
    }
    private boolean has_changed(ArrayList<Order> new_orders)
    {
        int size;
        if ((size = old_orders.size()) == new_orders.size())
        {
            for (int i =0; i <size; i++)
            {
                 if(!(old_orders.get(i).give_data_string().equals(new_orders.get(i).give_data_string())))
                 {
                     return true;
                 }
                 return false;
            }
        
        }
        return true;
    }
    public ArrayList<Order> get_data()
    {
        ArrayList<Order> order_from_file = new ArrayList<>();
        String line;
        try
        {
            fr = new FileReader(file);
            try
            {
                br = new BufferedReader(fr);
                while ((line = br.readLine()) != null)
                {
                    if(line != "")
                    {
                        order_from_file.add(retrive_order(line));
                    }
                }
                br.close();
            }
            catch (IOException e)
            {
            }
        }
        catch (FileNotFoundException ex)
        {
        }
        return order_from_file;
    }
    private Order retrive_order(String line)
    {
        String [] data = line.split("\t");
        boolean with_delivery = Boolean.parseBoolean(data[1]);
        boolean is_complete = Boolean.parseBoolean(data[7]);
        double total_cost = Double.parseDouble(data[8]);
        double special_discount = Double.parseDouble(data[9]);
        return new Order(data[0], with_delivery, data[2], data[3], data[4], make_array(data[5]), make_array(data[6]), is_complete, total_cost, special_discount);
    }
    
    private ArrayList<String> make_array(String data)
    {
        ArrayList<String> returner = new ArrayList<>();
        String[] strings = data.split(" ");
        for (String str : strings)
        {
            returner.add(str);
        }
        return returner;
    }
            
}
